package zhihui.mobile.presenter.register;

import android.content.Context;
import android.text.TextUtils;

import com.google.gson.Gson;
import org.json.JSONException;
import org.json.JSONObject;

import zhihui.mobile.R;
import zhihui.mobile.bean.phone.CodeParams;
import zhihui.mobile.bean.phone.PhoneCode;
import zhihui.mobile.bean.retrieve.RetrieveInfo;
import zhihui.mobile.config.Config;
import zhihui.mobile.interfaces.net.IHttpCallBackListener;
import zhihui.mobile.manager.phone.PhoneManager;
import zhihui.mobile.manager.register.RegisterManager;
import zhihui.mobile.ui.IView;
import zhihui.mobile.uitils.CheckUtils;

/**
 * Created by 张公铭 on 2017/7/13 0013.
 */

public class RetrievePasswordPresenter implements IRetrievePasswordContract.IRetrievePasswordPresenter
{
    private Context mContext;
    private IRetrievePasswordContract.IRetrievePasswordView retrievePasswordView;
    private PhoneCode curCode;

    @Override
    public void init(IView view, Context context)
    {
        mContext = context;
        retrievePasswordView = (IRetrievePasswordContract.IRetrievePasswordView) view;
    }

    @Override
    public void onResume()
    {
        retrievePasswordView.edPhoneFocuse();
    }

    @Override
    public void onStart()
    {

    }

    @Override
    public void onPause()
    {

    }

    @Override
    public void onStop()
    {

    }

    @Override
    public void onDestroy()
    {

    }

    @Override
    public void getPhoneCode(String phone)
    {
        String msg = CheckUtils.checkPhone(phone);
        if (!TextUtils.isEmpty(msg))
        {
            retrievePasswordView.showMessage(msg);
            retrievePasswordView.edPhoneFocuse();
            return;
        }

        CodeParams codeParams = new CodeParams();
        codeParams.setPhone(phone);
        codeParams.setType(3);
        retrievePasswordView.showLoading();
        PhoneManager.getIntance().sendMessage(codeParams, new IHttpCallBackListener()
        {
            @Override
            public void success(JSONObject result)
            {
                retrievePasswordView.hindLoading();
                try
                {
                    String state = result.getString("state");
                    String msg = result.getString("msg");
                    if (state.equals(Config.SUCCESS))
                    {
                        retrievePasswordView.startTimer();
                        Gson gson = new Gson();
                        curCode = gson.fromJson(result.getString("data"), PhoneCode.class);
                        retrievePasswordView.showMessage("验证码已发送，请注意查收！");
                        retrievePasswordView.edCodeFocuse();
                    } else
                    {
                        retrievePasswordView.showMessage(msg);
                    }
                } catch (JSONException e)
                {
                    retrievePasswordView.showMessage(mContext.getString(R.string.json_error) + e.getMessage());
                    e.printStackTrace();
                }
            }

            @Override
            public void failed(String message)
            {
                retrievePasswordView.hindLoading();
                retrievePasswordView.showMessage(message);
            }
        });
    }

    @Override
    public void submit(String phone, String code, String password)
    {
        String msg = CheckUtils.checkPhone(phone);
        if (!TextUtils.isEmpty(msg))
        {
            retrievePasswordView.showMessage(msg);
            retrievePasswordView.edPhoneFocuse();
            return;
        }

        msg = CheckUtils.checkPhoneCode(code, curCode);
        if (!TextUtils.isEmpty(msg))
        {
            retrievePasswordView.showMessage(msg);
            retrievePasswordView.edCodeFocuse();
            return;
        }

        msg = CheckUtils.checkPassword(password);
        if (!TextUtils.isEmpty(msg))
        {
            retrievePasswordView.showMessage(msg);
            retrievePasswordView.edPasswordFocuse();
            return;
        }

        RetrieveInfo retrieveInfo = new RetrieveInfo();
        retrieveInfo.setPhone(phone);
        retrieveInfo.setCede(code);
        retrieveInfo.setPassword(password);

        retrievePasswordView.showLoading();
        RegisterManager.getInstance().retrievePassword(retrieveInfo, new IHttpCallBackListener()
        {
            @Override
            public void success(JSONObject result)
            {
                retrievePasswordView.hindLoading();
                try
                {
                    String state = result.getString("state");
                    String msg = result.getString("msg");
                    if (state.equals(Config.SUCCESS))
                    {
                        retrievePasswordView.showSuccessDialog(msg);
                    } else
                    {
                        retrievePasswordView.showMessage(msg);
                    }

                } catch (JSONException e)
                {
                    retrievePasswordView.showMessage(mContext.getString(R.string.json_error));
                    e.printStackTrace();
                }
            }

            @Override
            public void failed(String message)
            {
                retrievePasswordView.hindLoading();
                retrievePasswordView.showMessage(message);
            }
        });

    }

}
